package cn.tlb.retrofit.demo;

import android.test.AndroidTestCase;
import android.util.Log;

import cn.tlb.utils.ClassParser;
import retrofit.Call;
import retrofit.Callback;
import retrofit.GsonConverterFactory;
import retrofit.Response;
import retrofit.Retrofit;

/**
 * Created by 汤林冰 on 2015/12/18 20:25.
 */
public class RetrofitTest extends AndroidTestCase {
    private static final String TAG = RetrofitTest.class.getSimpleName();

    // json对象
    public void test1() {
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://192.168.47.146:8080")
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        UserService userService = retrofit.create(UserService.class);
        String classText = ClassParser.getClassText(userService);
        Log.i(TAG, classText);

        Call<User> userCall = userService.getUser("json1");
        userCall.enqueue(new Callback<User>() {
            @Override
            public void onResponse(Response<User> response, Retrofit retrofit) {
                Log.e(TAG, "onResponse: " + response.body().toString());
            }

            @Override
            public void onFailure(Throwable t) {
                Log.e(TAG, "onFailure: " + t.getMessage());
            }
        });
    }

    // json数组
    public void test2() {

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(BaiduSearchService.URL)
                .build();

        BaiduSearchService service = retrofit.create(BaiduSearchService.class);
        //Unable to create converter for class java.lang.String
        Call<String> userCall = service.getData("Restrofit");
        userCall.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Response<String> response, Retrofit retrofit) {
                Log.e(TAG, "onResponse: " + response.body());
            }

            @Override
            public void onFailure(Throwable t) {
                Log.e(TAG, "onFailure: " + t.getMessage());
            }
        });
    }
}
